package edu.szte.ovrt.datahandler.ui.console;

import java.util.Map;

/**
 * Represents an abstract Dialog windows, used for read input from console.
 * 
 * @author Péter Török
 */
public class Dialog extends UIBase {

	protected final DialogInputMap m_dialogMap = new DialogInputMap();
	public final static String defaultKey = "_default";

	protected Dialog(final String title, final Boolean emptyValid) {
		m_dialogMap.add(defaultKey, title, emptyValid);
	}

	protected Dialog(final String title) {
		this(title, false);
	}

	protected Dialog(final Map<String, String> form) {
		for (final Map.Entry<String, String> item : form.entrySet()) {
			m_dialogMap.add(item.getKey(), item.getValue());
		}
	}

	protected final void run() {
		for (final DialogInputItem item : m_dialogMap.values()) {
			while (!item.read()) {
				showError("Invalid input, please try again!");
			}
		}
	}

	public String getValue(final String key) {
		run();
		return m_dialogMap.get(key).getValue();
	}

	public String getValue() {
		run();
		return m_dialogMap.get(defaultKey).getValue();
	}
}
